<%#
Copyright (C) 2017 fuyumi <280604399@qq.com>
Licensed to the public under the GNU Affero General Public License v3.
%>

<%+cbi/valueheader%>
<style>
    .generator textarea {
        display: block;
        width: auto;
        font-family: Arial;
    }
    .lb {
        display: inline-block;
        background-color: #ddd;
        padding: 4px 11px;
        font-size: 16px;
        font-family: Arial;
    }
    .generator input {
        display: inline-block;
        visibility: hidden;
        width: 0 !important;
        margin: 1rem;
    }
</style>

<div class="generator">
    <input class="file-upload" type="file" id="file-upload-d" accept=".pcapng"><label class="lb" for="file-upload-d">Version D</label>
    <input class="file-upload" type="file" id="file-upload-p" accept=".pcapng"><label class="lb" for="file-upload-p">Version P</label>
    <textarea id="display" rows="15" style="min-width: 300px;">
    </textarea>
</div>

<script type="text/javascript">
    var params;

    window.onload = function () {
        var el = document.querySelectorAll(".file-upload");
        for (var i = 0, len = el.length; i < len; i++) {
            el[i].addEventListener('click', function () {
                fileupload(function (file, method) {
                    if (file) {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            var data = e.target.result;
                            if (method === 'd') {
                                params = re_d(data);
                                display();
                            } else if (method === 'p') {
                                params = re_p(data);
                                display();
                            };
                        }
                        reader.readAsArrayBuffer(file);
                    }
                }, '.pcapng', this);
            })
        }
    };

    function fileupload (callback, accept, fileSelector) {
        var method = fileSelector.getAttribute('id').slice(-1);
        fileSelector.addEventListener('change', function () {
            var files = fileSelector.files;
            if (files.length) {
                callback(files[0], method);
            } else {
                callback(null, null);
            }
        });
    }

    function display() {
        var print = '';
        for (var i = 0; i < params[0].length; i++) {
            if (params[0][i] == 'mac' || params[0][i] == 'ror_version'){
                print += (params[0][i] + ' = ' + params[1][i] + '\'\n');
            } else {
                print += (params[0][i] + ' = \'' + params[1][i] + '\'\n');
            }
        };
        print = print.slice(0, -1);
        document.querySelector("#display").value = print;
    }

    function hexEncode(array) {
        return array.map(function (byte) {
            return ('0' + (byte & 0xFF).toString(16)).slice(-2);
        }).join('')
    }

    String.prototype.hex2a = function () {
        var str = '';
        for (var i = 0; i < this.length; i += 2)
            str += String.fromCharCode(parseInt(this.substr(i, 2), 16));
        return str
    }

    String.prototype.hex2o = function () {
        var str = '';
        for (var i = 0; i < this.length; i += 2)
            str += (parseInt(this.substr(i, 2), 16) + '.');
        return str
    }

    function re_d (text) {
        var int8array = new Uint8Array(text);
        var textarray = Array.from(int8array);
        text = hexEncode(textarray);
        var re1 = /f000f000[00-ff]{8}0[37]01/;
        var r1 = text.match(re1);
        var offset = text.indexOf(r1) + 16;
        var re2 = /0000[00-ff]{4}/;
        var r2 = text.substring(offset + 668, offset + 676).match(re2);
        if (r2 !== null) {
            var ror_version = true;
        } else {
            var ror_version = false;
        }
        var username_len = (parseInt(text.substring(offset + 6, offset + 8), 16) - 20)*2;
        var username = text.substring(offset + 40, offset + 40 + username_len).hex2a();
        var server = text.substring(offset - 24, offset -16).hex2o().slice(0, -1);
        var password = 'CHANGEME!!!';
        var CONTROLCHECKSTATUS = '\\x' + text.substring(offset + 112, offset + 114);
        var ADAPTERNUM = '\\x' + text.substring(offset + 114, offset + 116);
        var host_ip = text.substring(offset + 162, offset + 170).hex2o().slice(0, -1);
        var IPDOG = '\\x' + text.substring(offset + 210, offset + 212);
        var host_name = 'fuyumi';
        var PRIMARY_DNS = text.substring(offset + 284, offset + 292).hex2o().slice(0, -1);
        var dhcp_server = text.substring(offset + 292, offset + 300).hex2o().slice(0, -1);
        var AUTH_VERSION = '\\x' + text.substring(offset + 620, offset + 622) + '\\x' + text.substring(offset + 622, offset + 624);
        if (ror_version) {
            var mac = '0x' + text.substring(offset + 656, offset + 668);
        } else {
            var mac = '0x' + text.substring(offset + 640, offset + 652);
        }
        var host_os = 'Windows 10';
        var re3 = /f000f000.{8}07..28000b01..../g;
        var r3 = text.match(re3);
        for (var i = r3.length - 1; i >= 0; i--) {
            if(r3[i].slice(-4)!='0f27')
                var KEEP_ALIVE_VERSION = r3[i].slice(-4).replace(/../ig, function (s) {return '\\x' + s});
        };
        var params1 = ['server','username','password','CONTROLCHECKSTATUS','ADAPTERNUM','host_ip','IPDOG','host_name','PRIMARY_DNS','dhcp_server','AUTH_VERSION','mac','host_os','KEEP_ALIVE_VERSION', 'ror_version']
        var params2 = [server,username,password,CONTROLCHECKSTATUS,ADAPTERNUM,host_ip,IPDOG,host_name,PRIMARY_DNS,dhcp_server,AUTH_VERSION,mac,host_os,KEEP_ALIVE_VERSION, String(ror_version).charAt(0).toUpperCase() + String(ror_version).slice(1)]
        return [params1, params2]
    }

    function re_p (text) {
        var int8array = new Uint8Array(text);
        var textarray = Array.from(int8array);
        text = hexEncode(textarray);
        var re1 = /07[00-ff]{2}60000300/;
        var r1 = text.match(re1);
        var offset = text.indexOf(r1);
        var server = text.substring(offset - 24, offset -16).hex2o().slice(0, -1);
        var pppoe_flag = '\\x' + text.substring(offset + 38, offset + 40);
        var re2 = /07.{2}28000b..(..)02/;
        var keep_alive2_flag = '\\x' + text.match(re2)[1];
        var params1 = ['server','pppoe_flag','keep_alive2_flag'];
        var params2 = [server,pppoe_flag,keep_alive2_flag]
        return [params1,params2]
    }
</script>

<%+cbi/valuefooter%>
